package com.hunlian.makelove.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hunlian.makelove.R;
import com.hunlian.makelove.bean.UpdateBean;
import com.hunlian.makelove.common.AppUtils;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.ServiceInterfaceDef;
import com.hunlian.makelove.net.api.UpdataServiceVisit;
import com.hunlian.makelove.tools.LogTools;
import com.hunlian.makelove.update.AppUpdate;
import com.hunlian.makelove.update.Update;

import static com.hunlian.makelove.R.color.base_pink_color;

public class AboutActivity extends BaseActivity {

    private AboutActivity mActivity;
    private String mTitle;
    private RelativeLayout mRlProtocol;
    private RelativeLayout mRlIntro;
    private RelativeLayout mRlVersion;
    private TextView mTvVersion;
    private Update update = null;
    //是否有新版本要升级，有为true,没有为false
    private boolean mHasNewVersion = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        mActivity = this;
        mTitle = "关于伉俪";

        RelativeLayout ll_title = (RelativeLayout) this.findViewById(R.id.ll_title);
        TextView mTvTitle = (TextView) ll_title.findViewById(R.id.title);
        mTvTitle.setText(mTitle);
        Button btnR = (Button) ll_title.findViewById(R.id.btn_nav_right);
        btnR.setVisibility(View.GONE);
        ImageButton mBtnBack = (ImageButton) ll_title.findViewById(R.id.back);
        mBtnBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mActivity.finish();
            }

        });

        mRlProtocol = (RelativeLayout) this.findViewById(R.id.rl_protocol);
        mRlIntro = (RelativeLayout) this.findViewById(R.id.rl_intro);
        mRlVersion = (RelativeLayout) this.findViewById(R.id.rl_version);
        mTvVersion = (TextView) findViewById(R.id.tv_version);

        mTvVersion.setText(AppUtils.getVersionName(mActivity));

        setListener();

        ifUpdataVersions();
    }

    private void setListener(){
        mRlProtocol.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mActivity,WebActivity.class);
                intent.putExtra("title","服务协议");
                intent.putExtra("url", ServiceInterfaceDef.URL_SERVICE_PROTOCOL);
                startActivity(intent);
            }
        });

        mRlIntro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mActivity,WebActivity.class);
                intent.putExtra("title","伉俪介绍");
                intent.putExtra("url", ServiceInterfaceDef.URL_INTRO);
                startActivity(intent);
            }
        });

        mRlVersion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mHasNewVersion){
                    // 弹出更新的对话框
                    update.showUpdateDialog();
                }
            }
        });

    }


    /**
     * 检查是否有新版本
     */
    private void ifUpdataVersions() {
        try {
            String pkName = this.getPackageName();
            int versionCode = this.getPackageManager()
                    .getPackageInfo(pkName, 0).versionCode;

            UpdataServiceVisit visit = new UpdataServiceVisit(mActivity);
            visit.setParamters(pkName, versionCode + "");
            //System.out.println("软件升级请求参数： "+visit.getParamter());
            visit.getResult(new NetVisitCallback<UpdateBean,Object>(mActivity) {

                @Override
                public void onSuccess(UpdateBean bean) {

                    String needToUpdate = bean.getNeedToUpdate();
                    if (!needToUpdate.equals("0")) {
                        mHasNewVersion = true;
                        mTvVersion.setTextColor(getResources().getColor(base_pink_color));
                        mTvVersion.setText("有新版本"+bean.getVersionName());

                        AppUpdate updateBean = new AppUpdate();
                        updateBean.setType(needToUpdate);
                        updateBean.setLog(bean.getChangeLog());
                        updateBean.setDownloadUrl(bean.getDownloadUrl());
                        updateBean.setVersion(bean.getVersionName());

                        update = new Update();
                        update.checkUpdate(mActivity);
                        update.setUpdate(updateBean);
                        // 弹出更新的对话框
                        //update.showUpdateDialog();
                    }else if(needToUpdate.equals("0")){
                        mHasNewVersion = false;
                    }
                }

                @Override
                public void onFailure(String errCode, String errMessage,Object obj) {
                    LogTools.i("版本更新反馈数据：" + errMessage);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(update!=null){
            update.destroy();
            update = null;
        }
    }
}
